package com.pai4j.pgc.service.resource.convert;
import com.pai4j.common.exception.BusinessException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 *  资源转图片工厂类
 *
 * @author: CYM-pai
 * @date: 2025/10/27 17:33
 **/
@Component
public class ResourceConvertImageFactory implements InitializingBean {

    @Autowired
    private WebApplicationContext webApplicationContext;

    private static final Map<String, AbstractResourceConvertImageService> serviceMap = new HashMap<>();

    /**
     * 初始化资源转图片工厂服务集
     */
    private void initServiceMap() {
        serviceMap.put(ResourceFormatEnum.PDF.name(), new PDFResourceConvertImageService());
        serviceMap.put(ResourceFormatEnum.PPT.name(), new PPTResourceConvertImageService());
        serviceMap.put(ResourceFormatEnum.DOC.name(), new DocResourceConvertImageService());
        serviceMap.put(ResourceFormatEnum.EXCEL.name(), new ExcelResourceConvertImageService());
    }

    /**
     * 根据 resourceFormat 获取 AbstractResource2ImageService
     * @param resourceFormat
     * @return
     */
    public static AbstractResourceConvertImageService getConvertService(String resourceFormat) {
        AbstractResourceConvertImageService resource2ImageService = serviceMap.get(resourceFormat);
        if (resource2ImageService == null) {
            throw new BusinessException("不支持的behaviorType");
        }
        return resource2ImageService;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        initServiceMap();
    }


}
